

using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Linq;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;
using Abp.Domain.Services;

using Yozeev;
using Yozeev.BusinessLogic;


namespace Yozeev.BusinessLogic.DomainService
{
    /// <summary>
    /// Schedule领域层的业务管理
    ///</summary>
    public class ScheduleManager :YozeevDomainServiceBase, IScheduleManager
    {
		
		private readonly IRepository<Schedule,Guid> _repository;

		/// <summary>
		/// Schedule的构造方法
		///</summary>
		public ScheduleManager(
			IRepository<Schedule, Guid> repository
		)
		{
			_repository =  repository;
		}


		/// <summary>
		/// 初始化
		///</summary>
		public void InitSchedule()
		{
			throw new NotImplementedException();
		}

		// TODO:编写领域业务代码

		public async Task<bool> ScheduleInsertAsync(List<Schedule> data)
		{
			if (data == null || data.Count == 0)
			{
				return true;
			}
			try
			{
				foreach (var entity in data)
				{
					await _repository.InsertAsync(entity);
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}


		 
		  
		 

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public List<Schedule> GetScheduleList()
		{
			var list = _repository.GetAll().ToList();
			return list;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public Schedule GetScheduleById(Guid Id)
		{
			//var scheduleId = new Guid(Id);
			return _repository.FirstOrDefault(m => m.Id == Id);
		}

        public async Task UpdateSchedule(Schedule schedule)
        {
            await _repository.UpdateAsync(schedule);
        }

		public async Task<DateTime> GetStartTimeByScheduleIdAsync(string scheduleId)
		{
			var schedule = await _repository.GetAll()
				.Where(s => s.Id.ToString().Equals(scheduleId))
				.FirstOrDefaultAsync();
			return schedule == null ? DateTime.MinValue : schedule.StartTime;
		}
	}
}
